package com.liu.db;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.liu.crypto.Aes;
import com.liu.keeppwd.SysState;
import com.liu.model.Ac;
import com.liu.model.Pwd;
import com.liu.myerror.KeyEmptyError;

public class DbPwd {
	static String[] zd = { "pUUID", "pwd", "createTime", "Xh" };
	static int[] type = { 1, 1, 2, 2 };

	public static String getDbCreateS() {
		String res = "create table pwd(";
		res += "UUID char(32) primary key,";

		for (int i = 0; i < zd.length; i++) {
			res += zd[i];
			if (type[i] == 1) {
				res += " varchar(2000)";
			} else {
				res += " bigint";
			}
			if (i != zd.length - 1) {
				res += ",";
			}
		}
		res += ")";
		return res;
	}

	public static List<Object[]> AddRawData(List<Pwd> acLst) {

		List<Object[]> res = new ArrayList<Object[]>();

		String res1 = "delete from pwd where pUUID=?";
		Object[] obj1 = new Object[2];
		obj1[0] = acLst.get(0).getpUUID();
		obj1[1] = res1;
		res.add(obj1);
		for (int i = 0; i < acLst.size(); i++) {
			String res2 = "insert into pwd(UUID,pUUID,pwd,createTime,Xh) values(?,?,?,?,?)";
			Pwd ac = acLst.get(i);
			Object[] obj2 = new Object[6];
			obj2[0] = ac.getUUID();
			obj2[1] = ac.getpUUID();
			obj2[2] = ac.getPwd();
			obj2[3] = ac.getCreateTime().getTime();
			obj2[4] = ac.getXh();
			obj2[5] = res2;
			res.add(obj2);
		}
		return res;
	}

	public static List<Object[]> Update(Pwd ac, String key) throws Exception {
		if (key == null || key.equals("")) {
			throw new KeyEmptyError();
		}
		List<Object[]> resLst = new ArrayList<Object[]>();
		List<Object> objs = new ArrayList<Object>();
		String res = "update pwd set ";

		res += "pwd=?";
		objs.add(Aes.Encrypt(key, ac.getPwd()));

		res += " where UUID=?";
		objs.add(ac.getUUID());
		objs.add(res);
		resLst.add(objs.toArray());
		Object[] obj2 = new Object[3];
		obj2[0] = "1";
		obj2[1] = "syncflag";
		obj2[2] = "update quickSave set pvalue=? where pkey=?";
		resLst.add(obj2);
		return resLst;
	}

	public static List<Object[]> Add(Pwd ac, String key) throws Exception {
		if (key == null || key.equals("")) {
			throw new KeyEmptyError();
		}

		String res = "insert into pwd(UUID,pUUID,pwd,createTime,Xh) values(?,?,?,?,?)";
		Object[] obj = new Object[6];
		obj[0] = ac.getUUID();
		obj[1] = ac.getpUUID();
		obj[2] = Aes.Encrypt(key, ac.getPwd());
		obj[3] = (new Date()).getTime();
		obj[4] = ac.getXh();
		obj[5] = res;
		List<Object[]> resLst = new ArrayList<Object[]>();
		resLst.add(obj);
		Object[] obj2 = new Object[3];
		obj2[0] = "1";
		obj2[1] = "syncflag";
		obj2[2] = "update quickSave set pvalue=? where pkey=?";
		resLst.add(obj2);
		return resLst;
	}

	public static List<Pwd> SelectWithDecrypt(SQLiteDatabase dbms, String pUUID, String order, String key)
			throws KeyEmptyError {
		if (key == null || key.equals("")) {
			throw new KeyEmptyError();
		}
		List<Pwd> res = Select(dbms, pUUID, order);
		for (int i = 0; i < res.size(); i++) {
			Pwd a = res.get(i);
			try {
				a.setPwd(Aes.Decrypt(key, a.getPwd()));
			} catch (Exception e) {
				a.setPwd("");
			}
		}
		return res;

	}

	public static List<Pwd> Select(SQLiteDatabase dbms, String pUUID, String order) {
		List<Pwd> res = new ArrayList<Pwd>();
		String selStr = "select UUID,pUUID,pwd,createTime,Xh from pwd where pUUID=? order by Xh " + order;
		Cursor cursor = dbms.rawQuery(selStr, new String[] { pUUID });
		while (cursor.moveToNext()) {
			Pwd ac = new Pwd();
			ac.setUUID(cursor.getString(0));
			ac.setpUUID(cursor.getString(1));
			ac.setPwd(cursor.getString(2));
			ac.setCreateTime(new Date(cursor.getLong(3)));
			ac.setXh(cursor.getInt(4));
			res.add(ac);
		}
		return res;
	}

	public static Object[] CleanData() {
		List<Object> objs = new ArrayList<Object>();
		String res = "delete from pwd ";
		objs.add(res);
		return objs.toArray();
	}

	public static Object[] Delete(String pUUID) {
		List<Object> objs = new ArrayList<Object>();
		String res = "delete from pwd where pUUID=?";
		objs.add(pUUID);
		objs.add(res);
		return objs.toArray();
	}

}
